//
//  CollectViewController.m
//  SugarPile
//
//  Created by LC on 16/2/27.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseManager.h"
#import "Movie.h"
#import "HotDetailsViewController.h"
@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *collecttableView;
@property (nonatomic, retain) NSMutableArray *dataArr; /**< 数据库数组 */
@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 打开表
//     DataBaseManager *dbManger = [DataBaseManager shareInstance];
//    [dbManger openDB];
     self.dataArr = [[DataBaseManager shareInstance] select];
    NSLog(@"%@", _dataArr);
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"清空收藏" style:UIBarButtonItemStylePlain target:self action:@selector(qingchu)] autorelease];
    
    self.collecttableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _collecttableView.delegate = self;
    _collecttableView.dataSource = self;
    [self.view addSubview:_collecttableView];
    [_collecttableView release];
    [_collecttableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}
- (void)qingchu
{
    // 提示控制器
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清空收藏么 亲" message:@"确认清空么 亲" preferredStyle:UIAlertControllerStyleAlert];
    
    
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"好!" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        // 清除数据库中文件
        for (Movie *m in _dataArr) {
            [[DataBaseManager shareInstance] deleteMovie:m];
        }
        // 清空数组
        [_dataArr removeAllObjects];
        [_collecttableView reloadData];
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"再等等" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    Movie *m = _dataArr[indexPath.row];
    cell.textLabel.text = m.movieName;
    return cell;
}
// 点击跳转方法
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    HotDetailsViewController *hotVC = [[[HotDetailsViewController alloc] init] autorelease];
     hotVC.hidesBottomBarWhenPushed = YES;
    Movie *m = _dataArr[indexPath.row];
    // 字符串转NSNumber
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
     id result = [f numberFromString:m.pubdate];

    hotVC.detailsId = result;
    [self.navigationController pushViewController:hotVC animated:YES];
}
// 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
// 通过编辑状态判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 通过参数editingStyle判断 执行不同操作
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除 分两个部分 数据/视图
        // 先操作数据 再同步更新视图
        // 1.从数组中 删除数据
        NSLog(@"!!%@", _dataArr);
        Movie *m = _dataArr[indexPath.row];
        NSLog(@"$$%@", m);
        [[DataBaseManager shareInstance] deleteMovie:m];
        [_dataArr removeObjectAtIndex:indexPath.row];
        NSLog(@"@@%@", _dataArr);
        // 2.同步视图
        // 参数1: 试图操作cell 的位置
        // 参数2: 操作的动画选项
        [_collecttableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//        [_collecttableView reloadData];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 默认编辑样式为 删除
    //    return UITableViewCellEditingStyleInsert; // 添加/插入
    //    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
    return UITableViewCellEditingStyleDelete;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    DataBaseManager *dbManger = [DataBaseManager shareInstance];
//    [dbManger openDB];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
